<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input v-model="uanme" type="text" />
    </div>
    <div>
      <span>年龄:</span>
      <input v-model.number="uage" type="number" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="usex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="addEditFn()" >添加/修改</button>
    </div>
    <div>
      <table
        border="1"
        cellpadding="10"
        cellspacing="0"
      >
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item,index) in arr" :key="index">
          <td>{{ index + 1}}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.sex }}</td>
          <td>
            <button >删除</button>
            <button @click="editFn(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
   data() {
    return {
      arr :[
        { 
          id : '1',
          name : 'harry',
          age : 25,
          sex : '男'
        },
        {
          id : '2',
          name : '影妹妹',
          age : 22,
          sex : '女'
        },
      ],
      uanme:'',
      uage:0,
      usex:'',
      status : true
    }
   },
   methods:{
  
    addEditFn(){
      if(this.uanme.trim() === '' || this.uage <= 0) {
        return alert('请输入完整信息哦')
      }
      if(this.status === true) {
         this.arr.push(
        {
          name : this.uanme,
          age : this.uage,
          sex : this.usex,
        }
      )
      }
      if(this.status === false) {
        this.arr.splice(index,0,{
          name : this.uanme,
          age : this.uage,
          sex : this.usex,
        })
      }
     
      this.status = !this.status
      this.uanme = ''
      this.uage = 0
      this.usex = ''
    },
    editFn(ind){
      this.uanme = this.arr[ind].name
      this.uage = this.arr[ind].age
      this.usex = this.arr[ind].sex
    }
   }
}
</script>


